package com.sunshine.wrongquestionset.activity.exercise

import android.app.Application
import android.content.Context
import android.content.SharedPreferences

import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import com.sunshine.wrongquestionset.room.entity.QuestionEntity

import com.sunshine.wrongquestionset.room.repository.MyRepository
import java.util.*

/**
 * @author sunshine
 * @date 2021/3/27
 */
class ExerciseViewModel(application: Application): AndroidViewModel(application) {
    private val repository: MyRepository = MyRepository(application.applicationContext)

    private val sp: SharedPreferences = application.applicationContext.getSharedPreferences("com.sunshine.wrongquestionset_preferences", Context.MODE_PRIVATE)

    fun getAllQuestion(userId: String): LiveData<List<QuestionEntity>?> {
        return repository.getUserAllQuestion(userId)
    }

    fun getQuestion(questionId: Int): LiveData<QuestionEntity?> {
        return repository.getQuestionById(questionId)
    }

    fun updateFamiliarity(questionId: Int, newFamiliarity: Float) {
        repository.updateQuestionFamiliarity(questionId, newFamiliarity)
    }

    fun insertWrongQuestion(questionId: Int, score: Float, totalScore: Float, answerPath: String) {
        repository.insertWrongQuestion(questionId, Date().time, score, totalScore, answerPath)
    }

    fun insertQuestionInfo(questionId: Int, familiarity: Float) {
        repository.insertQuestionInfo(questionId, familiarity)
    }

    /**
     * 获取最大练习数量
     */
    fun getMaxExerciseCount(): Int {
        return sp.getInt("max_exercise_count", 10)
    }

    fun updateQuestion(questionEntity: QuestionEntity) {
        repository.updateQuestionByEntity(questionEntity)
    }
}